package com.example.day11_7_11_3.base;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.day11_7_11_3.base.BasePresenter;
import com.example.day11_7_11_3.base.BaseView;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {

    public P mPresenter;
    private Unbinder mUnbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(getLayout(), null);
        mUnbinder = ButterKnife.bind(this, inflate);
        mPresenter = initPresenter();
        if (mPresenter != null){
            mPresenter.bindView(this);
        }
        initView(inflate);
        initData();
        return inflate;
    }

    protected abstract P initPresenter();

    protected abstract void initData();

    protected abstract void initView(View inflate);

    protected abstract int getLayout();

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();

        mPresenter.onDestory();
        mPresenter = null;
    }
}
